//
//  HLVisitorLoginView.swift
//  weibo-snh
//
//  Created by 黄力 on 15/12/14.
//  Copyright © 2015年 HLTest. All rights reserved.
//

import UIKit

//设置协议
@objc protocol VisitorLoginViewDelegate: NSObjectProtocol {
    optional func visitorLogin()
    optional func visitorRegister()
}

class HLVisitorLoginView: UIView {
    
    //添加代理属性
    var delegate: VisitorLoginViewDelegate?
    
    //设置图片和文字
    func setVisitorLoginView(title: String, imageName: String?) {
        //根据图片名字是否为空来判断是否为主页面
        if imageName == nil {
            //如果是首页,给大图添加动画
            startAnimation()
        }else {
            largeView.image = UIImage(named: imageName!)
            titleLabel.text = title
            //隐藏房子视图和遮罩视图
            houseView.hidden = true
            coverView.hidden = true
        }
    }
    
    private func startAnimation () {
        //创建基本动画
        let baseAm = CABasicAnimation(keyPath: "transform.rotation")
        //设置执行动画的路劲
        baseAm.toValue = M_PI * 2
        //设置执行一次的时间
        baseAm.duration = 10
        //设置重复次数
        baseAm.repeatCount = MAXFLOAT
        //设置当视图没有显示的时候不移除动画
        baseAm.removedOnCompletion = false
        //添加动画
        largeView.layer.addAnimation(baseAm, forKey: nil)
        
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置界面
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置界面
    private func setupUI () {
        
        //添加子控件
        addSubview(largeView)
        addSubview(coverView)
        addSubview(houseView)
        addSubview(titleLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //遍历子控件,取消frame设置尺寸
        for subview in subviews {
            subview.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //房子图片
        addConstraint(NSLayoutConstraint(item: houseView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -40))
        //大图
        addConstraint(NSLayoutConstraint(item: largeView, attribute: .CenterX, relatedBy: .Equal, toItem: houseView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: largeView, attribute: .CenterY, relatedBy: .Equal, toItem: houseView, attribute: .CenterY, multiplier: 1, constant: 0))
        //文本
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: 230))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: 40))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .CenterX, relatedBy: .Equal, toItem: largeView, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Top, relatedBy: .Equal, toItem: largeView, attribute: .Bottom, multiplier: 1, constant: 15))
        //登录按钮
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: titleLabel, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Right, relatedBy: .Equal, toItem: registerBtn, attribute: .Left, multiplier: 1, constant: -20))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: titleLabel, attribute: .Bottom, multiplier: 1, constant: 15))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: 35))
        //注册按钮
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: loginBtn, attribute: .Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: loginBtn, attribute: .Height, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: loginBtn, attribute: .Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: titleLabel, attribute: .Right, multiplier: 1, constant: 0))
        //遮罩视图
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[coverView]-0-|", options: [], metrics: nil, views: ["coverView": coverView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[coverView]-(15)-[rebtn]", options: [], metrics: nil, views: ["coverView": coverView, "rebtn": registerBtn]))
        
        //设置背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        //给按钮设置响应事件
        loginBtn.addTarget(self, action: "clickLoginBtn", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "clickRegisterBtn", forControlEvents: .TouchUpInside)
    }
    
    //点击登录按钮
    @objc private func clickLoginBtn () {
        delegate?.visitorLogin!()
    }
    //点击注册按钮
    @objc private func clickRegisterBtn () {
        delegate?.visitorRegister!()
    }
    
    //房子图片
    lazy var houseView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //大图
    lazy var largeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //文本
    lazy var titleLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜", titleColor: UIColor.grayColor(), titleFont: 14)

    //登录按钮
    lazy var loginBtn: UIButton = {
        let btn = UIButton()
        //设置文字
        btn.setTitle("登录", forState: .Normal)
        //设置字体大小
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        //设置字体颜色
        btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable")?.stretchableImageWithLeftCapWidth(5, topCapHeight: 5), forState: .Normal)
        return btn
    }()
    //注册按钮
    lazy var registerBtn: UIButton = {
        let btn = UIButton()
        //设置文字
        btn.setTitle("注册", forState: .Normal)
        //设置字体大小
        btn.titleLabel?.font = UIFont.systemFontOfSize(15)
        //设置字体颜色
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable")?.stretchableImageWithLeftCapWidth(5, topCapHeight: 5), forState: .Normal)
        return btn
    }()
    //遮罩
    lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))

}
